package adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.customview.R;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

/**
 * Created by eyesee on 2017/4/24.
 */

public class FileManagerFileListAdapter extends BaseAdapter {

    private final Context mContext;
    private final List<String> mList;

    private static final int TYPE_FOLDER = 0;
    private static final int TYPE_VIDEO = 1;
    private static final int TYPE_MAX_COUNT = TYPE_VIDEO +1;

    private ArrayList<File> mFiles = new ArrayList();
    private TreeSet mVideos = new TreeSet();

    public FileManagerFileListAdapter(Context context, List<String> list) {
        this.mContext = context;
        this.mList = list;
    }

    public void addFolder (File file){
        mFiles.add(file);
        notifyDataSetChanged();
    }

    public void addVideos(File file){
        mFiles.add(file);
        mVideos.add(mFiles.size() - 1);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return mVideos.contains(position)? TYPE_VIDEO : TYPE_FOLDER;
    }

    @Override
    public int getViewTypeCount() {
        return TYPE_MAX_COUNT;
    }

    @Override
    public int getCount() {
        return mFiles==null?0:mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mFiles.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if (convertView==null){
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.filelist_item,null);
            holder.mTextView = (TextView) convertView.findViewById(R.id.filelist_textview);
            convertView.setTag(holder);
        }else {
            holder=((ViewHolder) convertView.getTag());
        }
        holder.mTextView.setText(mList.get(position));
        return convertView;
    }

    /*@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder ;
        int type = getItemViewType(position);

        if (convertView==null){
            holder = new ViewHolder();
            switch (type){
                case TYPE_FOLDER:
                    convertView = View.inflate(mContext, R.layout.filelist_item,null);
                    holder.mTextView = (TextView) convertView.findViewById(R.id.filelist_textview);
                    break;
                case TYPE_VIDEO:
                    convertView = View.inflate(mContext, R.layout.filelist_item_two,null);
                    holder.mTextView = (TextView) convertView.findViewById(R.id.filelist_textview);
                    holder.mImageView = (ImageView) convertView.findViewById(R.id.filelist_img);
                    break;
            }
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.mTextView.setText(mFiles.get(position).getName());
        return convertView;
    }*/

    class ViewHolder{

        public TextView mTextView;
        public ImageView mImageView;
    }
}
